package com.oauth.google.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.google.client.ui.tabs.GoogleDocPanel;
import com.oauth.google.client.ui.tabs.GoogleMailPanel;
import com.oauth.google.client.ui.tabs.GoogleOrkutPanel;
import com.oauth.google.client.ui.tabs.GooglePicasaPanel;
import com.oauth.google.client.ui.tabs.GoogleYouTubePanel;

public class GoogleTabPanel extends Composite {
	public static final String applicationName = "Google";

	@UiField
	DecoratedTabPanel tabPanel;

	@UiField
	DecoratedStackPanel stackPanel;

	@UiField
	DeckPanel deckPanel;

	private GoogleDocPanel mGoogleDocPanel;
	private GooglePicasaPanel mGooglePicasaPanel;
	private GoogleMailPanel mGoogleMailPanel;
	private GoogleYouTubePanel mGoogleYouTubePanel;
	private GoogleOrkutPanel mGoogleOrkutPanel;

	private static GoogleTabPanelUiBinder uiBinder = GWT
			.create(GoogleTabPanelUiBinder.class);

	interface GoogleTabPanelUiBinder extends UiBinder<Widget, GoogleTabPanel> {
	}

	public GoogleTabPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		stackPanel.setStackText(0, "<div class=\"stackText\">"
				+ applicationName + "</div>", true);

	}

	public void initTabPanel() {
		deckPanel.showWidget(0);
		mGoogleDocPanel = new GoogleDocPanel();
		mGooglePicasaPanel = new GooglePicasaPanel();
		mGoogleMailPanel = new GoogleMailPanel();
		mGoogleYouTubePanel = new GoogleYouTubePanel();
		mGoogleOrkutPanel = new GoogleOrkutPanel();

		tabPanel.add(mGoogleOrkutPanel, "Orkut");
		tabPanel.add(mGoogleDocPanel, "Google Docs");
		tabPanel.add(mGooglePicasaPanel, "Picasa");
		tabPanel.add(mGoogleMailPanel, "Gmail");
		tabPanel.add(mGoogleYouTubePanel, "YouTube");

		tabPanel.setAnimationEnabled(true);
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {

			@Override
			public void onSelection(SelectionEvent<Integer> pEvent) {
				if (pEvent.getSelectedItem() == 0) {
					mGoogleOrkutPanel.init();
				} else if (pEvent.getSelectedItem() == 1) {
					mGoogleDocPanel.init();
				} else if (pEvent.getSelectedItem() == 2) {
					mGooglePicasaPanel.init();
				} else if (pEvent.getSelectedItem() == 3) {
					mGoogleMailPanel.init();
				} else if (pEvent.getSelectedItem() == 4) {
					mGoogleYouTubePanel.init();
				} else {
					System.out.println("Not seelceted:"
							+ pEvent.getSelectedItem());
				}
			}

		});

		String tabIndex = Window.Location.getParameter("i");
		if (tabIndex != null) {
			try {
				tabPanel.selectTab(Integer.parseInt(tabIndex));
			} catch (NumberFormatException e) {
				e.printStackTrace();
				tabPanel.selectTab(0);
			}
		} else {
			tabPanel.selectTab(0); // default select first tab.
		}

		deckPanel.showWidget(1);
	}

}
